using Horizoncraft.script.Components.EnergyBlocks;
using Horizoncraft.script.Events;

namespace Horizoncraft.script.Components.Systems.BlockSystems.EnergyBlocks;
/// <summary>
/// 能量线缆系统
/// 每Tick向周围传输能量
/// </summary>
public class EnergyCableSystem : TickSystem
{
    public override void BlockTick(BlockTickEvent blockTickEvent, Component component)
    {
        if (component is EnergyUnitComponent energy)
        {
            if (energy.EnergyUnitValue < energy.Rate) return;
            int sv = energy.EnergyUnitValue;
            var result =
                blockTickEvent.Service.ChunkService.GetBlockAsSameComponent<EnergyUnitComponent>(blockTickEvent
                    .GlobalePos);
            foreach (var block in result)
            {
                if (energy.EnergyUnitValue < energy.Rate) continue;
                var cmp = block.GetComponent<EnergyUnitComponent>();
                if (cmp == null) continue;
                if (!cmp.InputAble) continue;
                if ((float)cmp.EnergyUnitValue / cmp.EnergyUnitMax >
                    (float)energy.EnergyUnitValue / energy.EnergyUnitMax) continue;

                int space = cmp.EnergyUnitMax - cmp.EnergyUnitValue;
                if (space <= 0) continue;
                
                if (space > energy.Rate)
                {
                    cmp.EnergyUnitValue += energy.Rate;
                    energy.EnergyUnitValue -= energy.Rate;
                }
                else
                {
                    cmp.EnergyUnitValue = cmp.EnergyUnitMax;
                    energy.EnergyUnitValue -= space;
                }
            }

            if (energy.EnergyUnitValue != sv)
            {
                blockTickEvent.SetUpdate();
            }
        }
    }
}